from langchain_openai import ChatOpenAI
from globle import envs

from langchain_core.runnables import RunnableParallel,RunnablePassthrough #直接流过的一个组件
from langchain_core.prompts import PromptTemplate as p
from langchain_core.output_parsers import StrOutputParser

llm = ChatOpenAI(
    model="deepseek-chat",  # 或具体模型名
    base_url="https://api.deepseek.com/v1",  # DeepSeek API 地址
    temperature = 2.0,
    max_tokens = 32
)
parser = StrOutputParser()

positive_chain = p.from_template("从积极角度分析{topic}，列举3个优点") | llm | parser
negative_chain = p.from_template("从批判角度分析{topic}，列举3个缺点") | llm | parser

# 构建并行链
together_chain = RunnableParallel({
    "positive": positive_chain,
    "negative": negative_chain,
    "topic": lambda x: x["topic"]
})
#RunnablePassthrough()#把输入原封不动输出


summary_chain = p.from_template("根据{positive}和{negative}总结一下{topic}") | llm | parser

total_chain = together_chain | summary_chain

r = total_chain.stream({"topic": "机器学习"})
for chunk in r:
    print(chunk,end= '')

